package mainpackage;
/*
 * MainServlet.java
 *
 * Created on October 7, 2008, 10:57 AM
 */

import com.google.gdata.util.AuthenticationException;
import com.google.gdata.util.ServiceException;
import java.io.*;
import java.net.MalformedURLException;
import java.util.Map;
import java.util.TreeMap;

import javax.servlet.*;
import javax.servlet.http.*;

/**
 *
 * @author sharif.ahmed
 * @version
 */
public class MainServlet extends HttpServlet {
    
    /** Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();

        Map<String, String[]> m = request.getParameterMap();//m.values().iterator().hasNext()
        Map<String, String> mm = new TreeMap<String, String>();
    
        for (Map.Entry<String, String[]> e : m.entrySet())
        {
            System.out.println(e.getKey() + ": " + e.getValue());
            mm.put( e.getKey(), e.getValue()[0] );
        }

        ContactsExampleParameters parameters = new ContactsExampleParameters(mm);
        ContactsExample example = null;
        try {
            example = new ContactsExample(parameters, out);
        } catch (AuthenticationException ex) {
            ex.printStackTrace();
        } catch (MalformedURLException ex) {
            ex.printStackTrace();
        }
        
         
        // TODO output your page here
        out.println("<html>");
        out.println("<head>");
        out.println("<title>Main Servlet</title>");
        out.println("</head>");
        out.println("<body>");
        out.println("<h1>Main Servlet at " + request.getContextPath () + "</h1>");
        
        out.println(request.getParameter("username")+"<br>");
        out.println(request.getParameter("password")+"<br>");
        out.println(request.getParameter("action")+"<br>");
        out.println(request.getParameter("id")+"<br>");
        out.println(request.getParameter("name")+"<br>");
        out.println(request.getParameter("email1")+"<br>");
        out.println(request.getParameter("im1")+"<br>");
        out.println(request.getParameter("phone1")+"<br>");
        
        try {
            
            example.processAction(example, parameters);
        } catch (IOException ex) {
            ex.printStackTrace();
        } catch (ServiceException ex) {
            ex.printStackTrace();
        }
        
        out.println("</body>");
        out.println("</html>");
        
        
        /* TODO output your page here
        out.println("<html>");
        out.println("<head>");
        out.println("<title>Servlet MainServlet</title>");
        out.println("</head>");
        out.println("<body>");
        out.println("<h1>Servlet MainServlet at " + request.getContextPath () + "</h1>");
        out.println("</body>");
        out.println("</html>");
         */
        out.close();
    }
    
    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }
    
    /** Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }
    
    /** Returns a short description of the servlet.
     */
    public String getServletInfo() {
        return "Short description";
    }
    // </editor-fold>
}
